package com.hermes.action.subject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.hermes.biz.common.model.item.CategorysDO;
import com.hermes.biz.common.model.item.ItemDO;
import com.hermes.biz.common.model.item.ItemPublicDO;
import com.hermes.biz.common.model.item.TagDO;
import com.hermes.biz.common.model.result.ItemResultDO;
import com.hermes.biz.common.model.user.UserPublicDO;
import com.hermes.biz.common.util.string.StringUtil;
import com.hermes.biz.dao.item.CategorysDAO;
import com.hermes.biz.dao.item.TagsDAO;
import com.hermes.biz.service.item.ItemService;
import com.hermes.common.action.BaseAction;
import com.hermes.common.util.LoginHelperUtil;
import com.hermes.common.util.ParameterParserUtil;

public class DetailDisplay extends BaseAction {

	/**
	 *
	*/
	public CategorysDAO categorysDAO;
	
	public TagsDAO tagsDAO;
	
	private static final long serialVersionUID = -5246087099224495968L;
			
	private Logger log = Logger.getLogger(DetailDisplay.class);
			
	private ItemService itemService;
			
	private ItemPublicDO result;
		
	public String username="";
	
	public UserPublicDO userPublicDO;
	
	public List<String> cates;

	public UserPublicDO getUserPublicDO() {
		return userPublicDO;
	}

	public void setUserPublicDO(UserPublicDO userPublicDO) {
		this.userPublicDO = userPublicDO;
	}

	public boolean isLogin = false;
			
	public long userid;

	public String url;
	
	public String[] cateids;
	
	public List<String> tagids;
	
	@Override
	public String execute() {
				
				//判断是否登录
				HttpServletRequest request = ServletActionContext.getRequest();
				LoginHelperUtil loginHelper = new LoginHelperUtil(request);
				if(loginHelper.hasLogined()){
					setLogin(true);
					userPublicDO = loginHelper.getUserPublicDO();
					username=loginHelper.getCurrentUserName();
				}
	
				String itemIdStr = ParameterParserUtil.getParameter("itemId");
				
				long itemId=0;
				
				result = new ItemPublicDO();
				
				if(StringUtil.isNumOnly(itemIdStr)){
						itemId = Long.parseLong(itemIdStr);
						ItemResultDO itemResultDO = itemService.getSingleItemByItemId(itemId);
						
						if(itemResultDO.isFailure()){
							return ERROR;
						}
						ItemDO itemDO = itemResultDO.getModule();
						result = new ItemPublicDO();
						result.setItemId(itemDO.getItemId());
						result.setDescription(itemDO.getDescription());
						result.setItemTitle(itemDO.getItemTitle());
						result.setTagId(itemDO.getTagId());
						result.setItemPicUrl(itemDO.getItemPicUrl());
						result.setIsWeb(itemDO.getIsWeb());
						result.setItemBuyUrl(itemDO.getBuy_url());
						
						log.error("URL:"+itemDO.getBuy_url());
						
						log.error("宝贝的标题是"+itemDO.getItemTitle());
						
						url = "detail.htm?itemId="+itemDO.getItemId()+"&"+"tagId="+itemDO.getTagId();
						
						try {
							url = URLEncoder.encode(url, "GBK");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						//categories and tags
						//tags and cetegories
						List<?> categorysList = new ArrayList<CategorysDO>();
						categorysList = categorysDAO.getCategorysList();
						
						if(null!=itemDO.getCategory()){
							cateids = itemDO.getCategory().split(",");
						}
						
						cates = new ArrayList<String>();
						if(cateids!=null&&cateids.length>0){
							for(String id : cateids){
								for(Object cdo : categorysList){
									if(((CategorysDO) cdo).getCategory_id().equals(id)){
										cates.add(((CategorysDO) cdo).getCategory_name());
									}
								}
							}
						}
						
						List<?> tagsList = new ArrayList<TagDO>();
						tagsList = tagsDAO.getTagsList();
						String[] tagsid = {};
						if(itemDO.getTags()!=null){
							tagsid =  itemDO.getTags().split(",");
							
						}
						tagids = new ArrayList<String>();
						if(null!=tagsid&&tagsid.length>0){
							for(String id : tagsid){
								for(Object tdo : tagsList){
									if(((TagDO) tdo).getTag_id().equals(id)){
										tagids.add(((TagDO) tdo).getTag_name());
									}
								}
							}
						}
						return SUCCESS;
				}
				log.error("获取宝贝没有成功");
				return ERROR;
			}
		
			public ItemService getItemService() {
				return itemService;
			}
		
			public void setItemService(ItemService itemService) {
				this.itemService = itemService;
			}

			public ItemPublicDO getResult() {
				return result;
			}

			public void setResult(ItemPublicDO result) {
				this.result = result;
			}
			public String getUsername() {
				return username;
			}

			public void setUsername(String username) {
				this.username = username;
			}

			public boolean isLogin() {
				return isLogin;
			}

			public void setLogin(boolean isLogin) {
				this.isLogin = isLogin;
			}

			public CategorysDAO getCategorysDAO() {
				return categorysDAO;
			}

			public void setCategorysDAO(CategorysDAO categorysDAO) {
				this.categorysDAO = categorysDAO;
			}

			public TagsDAO getTagsDAO() {
				return tagsDAO;
			}

			public void setTagsDAO(TagsDAO tagsDAO) {
				this.tagsDAO = tagsDAO;
			}
			public long getUserid() {
				return userid;
			}

			public void setUserid(long userid) {
				this.userid = userid;
			}

			public String getUrl() {
				return url;
			}

			public void setUrl(String url) {
				this.url = url;
			}
}
